package mest.controller.cxj;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import mest.Dto.cxj.WorkFileDto;
import mest.entity.modeling_dictionaries;
import mest.entity.work_file;
import mest.service.cxj.ModelingDictionariesService;
import mest.service.cxj.WorkFileService;
import mest.service.system.DBService;
import mest.util.NumberConst;
import mest.util.ResultObj;
import mest.util.mestUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("workFile")
@CrossOrigin
public class WorkFileController {

    @Autowired
    private WorkFileService workFileService;

    @Autowired
    private ModelingDictionariesService modelingDictionariesService;

    @Autowired
    private DBService dbService;

    /**
     * 修改
     *
     * @param jsonObject
     * @param request
     * @return
     */
    @PostMapping(value = "/update")
    public ResultObj update(@RequestBody JSONObject json, HttpServletRequest request) {
        
        Map<String, Object> map = json.toJavaObject(Map.class);
        map.put("theirDb", mestUtil.getCompanyDB(request));
        ResultObj resultObj = workFileService.update(map);
        return resultObj;
    }
    
    /**
     * 提供工序档案流水号
     *
     * @return
     */
    @GetMapping("getCode")
    public String getCode() {
        String theirDb = dbService.getCompanyDB();
        modeling_dictionaries modelingdictionaries = modelingDictionariesService.selectModelingDictionaries(theirDb);
        int workNo = modelingdictionaries.getWorkNo();
        return NumberConst.Work_No + "-" + workNo;
    }

    /**
     * 查询全部工序档案
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("selectAll")
    public String selectAll(@RequestParam Integer page, @RequestParam Integer limit,
                            @RequestParam(required = false) String workName) {
        ResultObj resultObj = null;
        String theirDb = dbService.getCompanyDB();
        try {
            List<WorkFileDto> workFileDtoList = workFileService.selectAllWorkFile(page, limit, workName, theirDb);
            Integer count = workFileService.selectCountWorkFile(workName, theirDb);
            resultObj = new ResultObj(0, "success", count, workFileDtoList);
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }

    /**
     * 添加工序档案
     *
     * @param workFile
     * @return
     */
    @PostMapping("insertModel")
    public String insertModel(@RequestBody work_file workFile) {
        ResultObj resultObj = null;
        String theirDb = dbService.getCompanyDB();
        try {
            if (workFileService.insertWorkFile(workFile,theirDb)) {
                modelingDictionariesService.updateModelingDictionariesByWorkNo(theirDb); //更新工序档案流水号
                resultObj = ResultObj.builder().code(0).msg("添加成功").build();
            } else {
                resultObj = ResultObj.builder().code(-1).msg("添加失败").build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }

    /**
     * 删除工序档案（假删）
     *
     * @param id
     * @return
     */
    @DeleteMapping("delModel")
    public String delModel(@RequestParam Integer id) {
        ResultObj resultObj = null;
        String theirDb = dbService.getCompanyDB();
        try {
            if (workFileService.deleteWorkFile(id, theirDb)) {
                resultObj = ResultObj.builder().code(0).msg("删除成功").build();
            } else {
                resultObj = ResultObj.builder().code(-1).msg("删除失败").build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }

}
